import Cookies from 'js-cookie';

const TokenKey = 'token';

export function getToken(key = TokenKey) {
  return Cookies.get(key);
}

export function setToken(value, overTime, key = TokenKey) {
  if (overTime) {
    Cookies.set(key, value, {
      expires: new Date(overTime * 1000),
    });
  } else {
    Cookies.set(key, value);
  }
}

export function removeAllCookies(...array) { // array为排除的
  Object.keys(Cookies.get()).forEach((val) => {
    if (array.every(v => v !== val)) {
      Cookies.remove(val);
    }
  });
}

export function removeAllLocalStorages(...array) { // array为排除的
  if (array) {
    Object.keys(window.localStorage).forEach((val) => {
      if (array.every(v => v !== val)) {
        window.localStorage.removeItem(val);
      }
    });
  } else {
    window.localStorage.clear();
  }
}
